#include "connectionserializationhelper.h"

#include "graphicdata.h"

#include <ieditoritem.h>

using namespace core;
using namespace swp;

ConnectionSerializationHelper::ConnectionSerializationHelper(
    GraphicData* data, const core::ConnectionData* connection) :
        m_data(data),
        m_connection(connection)
{
    Q_ASSERT(m_data != 0); // you can't pass null
}

ConnectionSerializationHelper::~ConnectionSerializationHelper()
{

}

bool ConnectionSerializationHelper::doActionForProperty(
    const QString& property, const QVariantList& value,
    const core::SerializationEngine& helper)
{
    bool ret = true;

    if (property == "connectionId")
    {
        m_connectionId = value.first().toUInt(&ret);
    }
    else if (property == "connector")
    {
        m_connector = QUuid(value.first().toString());
    }
    else if (property == "from")
    {
        m_from = QUuid(value.first().toString());
    }
    else if (property == "to")
    {
        m_to = QUuid(value.first().toString());
    }

    return ret;
}

void ConnectionSerializationHelper::deserializationFinished(bool ok)
{
    if (ok)
    {
        m_data->makeConnection(m_connectionId, m_connector, m_from, m_to);
    }
}

QHash<QString, QVariantList> ConnectionSerializationHelper::serializationData(
    const SerializationEngine& helper) const
{
     Q_ASSERT(m_connection != 0); // you can pass null for ConnectionData only when deserializing

    QHash<QString, QVariantList> ret;

    QVariantList list;

    list.append(QVariant(m_connection->ConnectionId));

    ret["connectionId"] = list;

    list.clear();

    list.append(QVariant(m_connection->Connector->id().toString()));

    ret["connector"] = list;

    list.clear();

    list.append(QVariant(m_connection->From->id().toString()));

    ret["from"] = list;

    list.clear();

    list.append(QVariant(m_connection->To->id().toString()));

    ret["to"] = list;

    return ret;
}
